package com.benio.ycfdisplayer.displayer;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * 加载图片请求
 * Created by benio on 2016/10/2.
 */
public class Request {
    private String mUri;
    private DisplayListener mDisplayListener;
    private Options mOptions;
    private Target mTarget;
    private static final Queue<Request> REQUEST_POOL = new ArrayDeque<>(0);

    public static Request obtain(String uri, Target target, Options options, DisplayListener displayListener) {
        Request request = REQUEST_POOL.poll();
        if (request == null) {
            request = new Request();
        }
        request.init(uri, target, options, displayListener);
        return request;
    }

    private Request() {
        // just create, instances are reused with recycle/init
    }

    private void init(String uri, Target target, Options options, DisplayListener displayListener) {
        this.mUri = uri;
        this.mDisplayListener = displayListener;
        this.mOptions = options;
        this.mTarget = target;
    }

    public String getUri() {
        return mUri;
    }

    public DisplayListener getDisplayListener() {
        return mDisplayListener;
    }

    public Options getOptions() {
        return mOptions;
    }

    public Target getTarget() {
        return mTarget;
    }

    public void recycle() {
        mUri = null;
        mDisplayListener = null;
        mOptions = null;
        mTarget = null;
        REQUEST_POOL.offer(this);
    }
}
